package com.irdstudio.rsp.rule.common;

import com.irdstudio.rsp.rule.dao.SOrgDao;
import com.irdstudio.rsp.rule.dao.domain.SOrg;
import com.irdstudio.sdk.admin.dao.SDicDao;
import com.irdstudio.sdk.admin.dao.domain.SDic;
import com.irdstudio.sdk.ssm.util.PathUtil;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

@Component
public class InitJsData {

    private static Logger logger = LoggerFactory.getLogger(InitJsData.class);

    @Autowired
    private SDicDao sDicDao;

    @Autowired
    private SOrgDao sOrgDao;

    private final String CITY_OPTION = "city";

    private final String ORG_OPTION = "org";

    @PostConstruct
    public void postCallback() {
        loadCityIntoJs();
        loadOrgIntoJs();
    }

    /**
     * 加载城市字典数据 增加分组字段
     */
    private void loadCityIntoJs() {
        String fileFullPath = PathUtil.getProjectRootPath() + "ffres/dict/city.js";
        logger.info("初始化城市字典数据到:" + fileFullPath);
        OutputStreamWriter osw = null;
        try {
            osw = new OutputStreamWriter(new FileOutputStream(fileFullPath), "UTF-8");
            StringBuffer sb = new StringBuffer();
            List<SDic> options = this.sDicDao.queryDictOption(CITY_OPTION);
            sb.append("var ").append("CUSTOM_" + CITY_OPTION.toUpperCase());
            sb.append("=[");
            options.forEach((option) -> {
                sb.append("{\"enname\":\"")
                        .append(option.getEnname())
                        .append("\",\"cnname\":\"")
                        .append(option.getCnname())
                        .append("\",\"parent\":\"")
                        .append(option.getMemo())
                    .append("\"},");
            });
            if (',' == sb.charAt(sb.length() - 1)) {
                sb.deleteCharAt(sb.length() - 1);
            }
            sb.append("];\r\n");
            //sb.append("function formatter_").append(CITY_OPTION.toUpperCase().toUpperCase()).append("(value, row, index){").append("return JLEUtil.formatter_dict(value,").append(CITY_OPTION.toUpperCase().toUpperCase()).append(");").append("};\r\n");
            osw.write(sb.toString());
        } catch (Exception var13) {
            logger.error("数据字典初始化出错", var13);
        } finally {
            IOUtils.closeQuietly(osw);
        }
        logger.info("字典数据初始化完成!");
    }

    /**
     * 从s_org加载机构下拉
     */
    private void loadOrgIntoJs() {
        String fileFullPath = PathUtil.getProjectRootPath() + "ffres/dict/org.js";
        logger.info("初始化机构字典数据到:" + fileFullPath);
        OutputStreamWriter osw = null;
        try {
            osw = new OutputStreamWriter(new FileOutputStream(fileFullPath), "UTF-8");
            StringBuffer sb = new StringBuffer();
            List<SOrg> options = this.sOrgDao.queryAll();
            sb.append("var ").append("CUSTOM_" + ORG_OPTION.toUpperCase());
            sb.append("=[");
            options.forEach((option) -> {
                sb.append("{\"enname\":\"")
                        .append(option.getOrganNo())
                        .append("\",\"cnname\":\"")
                        .append(option.getOrganName())
                        .append("\",\"parent\":\"")
                        .append(option.getSuporganNo())
                    .append("\"},");
            });
            if (',' == sb.charAt(sb.length() - 1)) {
                sb.deleteCharAt(sb.length() - 1);
            }
            sb.append("];\r\n");
            sb.append("function formatter_")
                    .append("CUSTOM_" + ORG_OPTION.toUpperCase())
                    .append("(value, row, index){")
                    .append("return JLEUtil.formatter_dict(value,")
                    .append("CUSTOM_" + ORG_OPTION.toUpperCase())
                    .append(");")
                    .append("};\r\n");
            osw.write(sb.toString());
        } catch (Exception var13) {
            logger.error("数据字典初始化出错", var13);
        } finally {
            IOUtils.closeQuietly(osw);
        }
        logger.info("字典数据初始化完成!");
    }
}
